<?php
namespace App\DBs;

use Illuminate\Support\Facades\DB;

class AddressDB extends BaseDB
{
    /**
     * 收货地址
     */

    public $tAddress = 'address';
    public $tCity = 'city';

    public function __construct()
    {
    }

    /**
     * 列表
     */
    public function getWxIndex($wx)
    {
        $models = DB::table($this->tAddress)->where('del',0)->where('wx',$wx)->get();
        if (!count($models)) {
            return array('code'=>'-1','msg'=>'no data');
        }
        $items = array(); $models = $this->jsonToArr($models);
        foreach ($models as $model) {
            $resProvince = DB::table($this->tCity)->where('id',$model['province'])->get();
            $resCity = DB::table($this->tCity)->where('id',$model['city'])->get();
            $resArea = DB::table($this->tCity)->where('id',$model['area'])->get();
            $provinces = count($resProvince) ? $this->jsonToArr($resProvince[0]) : '';
            $citys = count($resCity) ? $this->jsonToArr($resCity[0]) : '';
            $areas = count($resArea) ? $this->jsonToArr($resArea[0]) : '';
            $pname = $provinces ? $provinces['name'] : '';
            $cname = $citys ? $citys['name'] : '';
            $aname = $areas ? $areas['name'] : '';
            $items[] = [
                'id'=>$model['id'], 'wx'=>$model['wx'], 'uname'=>$model['uname'],
                'uphone'=>$model['uphone'], 'uprovince'=>$pname, 'ucity'=>$cname,
                'uarea'=>$aname, 'ustreet'=>$model['street'], 'check'=>$model['check'],
            ];
        }
        return array('code'=>'0','msg'=>'success','data'=>$items);
    }

    /**
     * 新增
     */
    public function setAdd($wx,$uname,$uphone,$uprovince,$ucity,$uarea,$ustreet)
    {
        $resProvince = DB::table($this->tCity)->where('name','like',$uprovince.'%')->where('level',1)->first();
        if (!$resProvince) { return array('code'=>'-1','msg'=>'省份不存在'); }
        $resCity = DB::table($this->tCity)->where('name','like',$ucity.'%')
            ->where('pid',$resProvince->id)->where('level',2)->first();
        if (!$resCity) { return array('code'=>'-2','msg'=>'城市不存在'); }
        $resArea = DB::table($this->tCity)->where('name','like',$uarea.'%')
            ->where('pid',$resCity->id)->where('level',3)->first();
        if (!$resArea) { return array('code'=>'-3','msg'=>'地区不存在'); }
        $proId = $resProvince->id; $cityId = $resCity->id; $areaId = $resArea->id;
        $items = [
            'wx'=>$wx, 'uname'=>$uname, 'uphone'=>$uphone, 'province'=>$proId,
            'city'=>$cityId, 'area'=>$areaId, 'street'=>$ustreet,
        ];
        $resOne = DB::table($this->tAddress)->where($items)->first();
        if ($resOne) {
            return array('code'=>'-4','msg'=>'地址已存在');
        }
        $items['check'] = 1; $items['created_at'] = time();
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tAddress)->where('wx',$wx)->update(['check'=>0]);
            if (!$resDb) {
                return array('code'=>'-2','msg'=>'取消失败');
            }
            $lastId = DB::table($this->tAddress)->insertGetId($items);
            if (!$lastId) {
                return array('code'=>'-5','msg'=>'新增失败');
            }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 选中
     */
    public function setCheck($id)
    {
        $resOne = DB::table($this->tAddress)->where('del',0)->where('id',$id)->first();
        if (!$resOne) {
            return array('code'=>'-1','msg'=>'地址不存在');
        }
        if ($resOne->check) {
            return array('code'=>'-2','msg'=>'地址已选中');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tAddress)->where('wx',$resOne->wx)->update(['check'=>0]);
            if (!$resDb) {
                return array('code'=>'-2','msg'=>'取消失败');
            }
            $resDb2 = DB::table($this->tAddress)->where('id',$id)->update(['check'=>1]);
            if (!$resDb2) {
                return array('code'=>'-2','msg'=>'选中失败');
            }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }

    /**
     * 删除
     */
    public function setDel($id)
    {
        $resOne = DB::table($this->tAddress)->where('del',0)->where('id',$id)->first();
        if (!$resOne) {
            return array('code'=>'-1','msg'=>'地址不存在');
        }
        //开启事务
        DB::beginTransaction();
        try {
            $resDb = DB::table($this->tAddress)->where('id',$id)->update(['del'=>1,'updated_at'=>time()]);
            if (!$resDb) {
                return array('code'=>'-2','msg'=>'删除失败');
            }
            //提交事务
            DB::commit();
            return array('code'=>'0', 'msg'=>'操作成功');
        } catch (\Exception $e) {
            //事务回滚
            DB::rollBack();
            return array('code'=>'-1', 'msg'=>'操作失败：'.$e);
        }
    }
}